{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "489f4d34",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "9718f22d",
   "metadata": {},
   "outputs": [],
   "source": [
    "R = np.zeros((10,10))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "251c6e85",
   "metadata": {},
   "outputs": [],
   "source": [
    "def ladder(f, n, a, b):\n",
    "    xArr = np.linspace(a, b, n + 1)\n",
    "    h = (b - a) / n\n",
    "    result = h * F(xArr) - h / 2 *(f(xArr[0]) + f(xArr[-1]))\n",
    "    return result\n",
    "\n",
    "def F(xArr):\n",
    "    return np.sum(f(xArr))\n",
    "\n",
    "def f(x):\n",
    "    return x ** 2 - x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "48a4e8a9",
   "metadata": {},
   "outputs": [],
   "source": [
    "a = -1\n",
    "b = 1\n",
    "for i in range(10):\n",
    "    R[i,0] = ladder(f, i+1, a, b)\n",
    "    for j in range(1,i):\n",
    "        R[i,j] = (4 ** j * R[i - 1, j - 1] - R[i,j - 1]) /  (4 ** j - 1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "1952ad7b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[2.        , 0.        , 0.        , 0.        , 0.        ,\n",
       "        0.        , 0.        , 0.        , 0.        , 0.        ],\n",
       "       [1.        , 0.        , 0.        , 0.        , 0.        ,\n",
       "        0.        , 0.        , 0.        , 0.        , 0.        ],\n",
       "       [0.81481481, 1.0617284 , 0.        , 0.        , 0.        ,\n",
       "        0.        , 0.        , 0.        , 0.        , 0.        ],\n",
       "       [0.75      , 0.83641975, 1.07674897, 0.        , 0.        ,\n",
       "        0.        , 0.        , 0.        , 0.        , 0.        ],\n",
       "       [0.72      , 0.76      , 0.8415144 , 1.08048285, 0.        ,\n",
       "        0.        , 0.        , 0.        , 0.        , 0.        ],\n",
       "       [0.7037037 , 0.7254321 , 0.76230453, 0.8427717 , 1.08141505,\n",
       "        0.        , 0.        , 0.        , 0.        , 0.        ],\n",
       "       [0.69387755, 0.70697909, 0.7266623 , 0.76287028, 0.84308504,\n",
       "        1.08164803, 0.        , 0.        , 0.        , 0.        ],\n",
       "       [0.6875    , 0.6960034 , 0.7077108 , 0.72696312, 0.76301109,\n",
       "        0.84316332, 1.08170626, 0.        , 0.        , 0.        ],\n",
       "       [0.68312757, 0.68895748, 0.69647313, 0.70788918, 0.72703792,\n",
       "        0.76304625, 0.84318288, 1.08172082, 0.        , 0.        ],\n",
       "       [0.68      , 0.6841701 , 0.68927663, 0.69658736, 0.7079335 ,\n",
       "        0.72705659, 0.76305504, 0.84318777, 1.08172446, 0.        ]])"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "R"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "90d31555",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
